//
//  YXYGroupHotNineTableViewCell.m
//  Topit
//
//  Created by dllo on 15/12/11.
//  Copyright © 2015年 YXY. All rights reserved.
//

#import "YXYGroupHotNineTableViewCell.h"
#import "YXYGroupHotNineCollectionViewCell.h"
@implementation YXYGroupHotNineTableViewCell
-(void)dealloc {
    [_flowLayout release];
    [_collectionView release];
    [_groupHotModel release];
    [_idImage release];
    [_idName release];
    [_ts release];
    [_name release];
//    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.idImage = [[UIImageView alloc] initWithFrame:CGRectZero];
        _idImage.layer.cornerRadius = 15;
        _idImage.clipsToBounds = YES;
        [self addSubview:_idImage];
        [_idImage release];
        
        self.idName = [[UILabel alloc] initWithFrame:CGRectZero];
        _idName.font = [UIFont systemFontOfSize:12];
        [self addSubview:_idName];
        [_idName release];
        
        self.ts = [[UILabel alloc] initWithFrame:CGRectZero];
        _ts.font = [UIFont systemFontOfSize:10];
        _ts.textColor = [UIColor grayColor];
        [self addSubview:_ts];
        [_ts release];
        
        self.name = [[UILabel alloc] initWithFrame:CGRectZero];
        _name.font = [UIFont systemFontOfSize:12];
        [self addSubview:_name];
        [_name release];
        
        self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_flowLayout];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
        [_collectionView registerClass:[YXYGroupHotNineCollectionViewCell class] forCellWithReuseIdentifier:@"YXYGroupHotNineCollectionViewCellIdentifier"];
        [self addSubview:_collectionView];
        [_flowLayout release];
        [_collectionView release];
    }
    return self;
}

//- (void)changeNightMode:(NSNotification *)messager {
//    
//    [self.collectionView reloadData];
//}

- (void)setGroupHotModel:(YXYGroupHotModel *)groupHotModel {
    if (_groupHotModel != groupHotModel) {
        [_groupHotModel release];
        _groupHotModel = [groupHotModel retain];
    }
    [_idImage sd_setImageWithURL:[NSURL URLWithString:groupHotModel.user[@"icon"][@"url"]]];
    [_idName nightMode];
    
    _idName.text = groupHotModel.user[@"name"];
    [_idName nightMode];
    
    _ts.text = groupHotModel.last_ts;
    [_ts nightMode];
    
    _name.text = groupHotModel.name;
    [_name nightMode];
    
//    [_collectionView reloadData];
}

-(void)layoutSubviews {
    [super layoutSubviews];
    _idImage.frame = CGRectMake(5, 5, 30, 30);
    _idName.frame = CGRectMake(_idImage.right + 5, _idImage.origin.y + 5, 200, 10);
    _ts.frame = CGRectMake(_idName.origin.x, _idName.bottom + 5, 200, 10);
    _name.frame = CGRectMake(_idImage.origin.x, _idImage.bottom + 10, self.width, 20);
    
    _flowLayout.itemSize = CGSizeMake(self.width / 3, 270 / 3);
    _flowLayout.minimumInteritemSpacing = 0;
    _flowLayout.minimumLineSpacing = 0;
    _collectionView.frame = CGRectMake(0, 61, self.width, 270);
    [_collectionView nightMode];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _groupHotModel.items.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    YXYGroupHotNineCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YXYGroupHotNineCollectionViewCellIdentifier" forIndexPath:indexPath];
    NSString *url = _groupHotModel.items[indexPath.item][@"icon"][@"url"];
    
    [cell setImageUrl:url];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSString *url = _groupHotModel.items[indexPath.item][@"icon"][@"url"];
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(8, 8, 200, 200)];
    [imgView sd_setImageWithURL:[NSURL URLWithString:url]];
    [SJAvatarBrowser showImage:imgView];
    [imgView release];
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
